package cn.tedu.charging.device.startup;

import cn.tedu.charging.common.pojo.constant.DeviceCacheKey;
import cn.tedu.charging.common.pojo.utils.JsonUtils;
import cn.tedu.charging.device.dao.repository.StationRepository;
import cn.tedu.charging.device.pojo.po.StationPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class StationDataInit implements ApplicationRunner {
    @Autowired
    private StationRepository stationRepository;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.debug("ApplicationRunner#run StationDataInit...");
        log.debug("缓存预热,在项目启动的时从数据库查询场站位置信息(经纬度),放到Redis里");
        List<StationPO> allStation = stationRepository.getAllStation();

        BoundGeoOperations station = redisTemplate.boundGeoOps(DeviceCacheKey.STATION_LOCATION_KEY);
        ValueOperations<String, Serializable> valueOperations = redisTemplate.opsForValue();
        HashOperations<String, Object, Object> hashOperations = redisTemplate.opsForHash();
        Map<String, Point> pointMap = new HashMap<>();
        for (StationPO stationPO : allStation) {
            BigDecimal BigDecimalLng = stationPO.getStationLng();
            BigDecimal BigDecimalLat = stationPO.getStationLat();
            double longitude = Double.parseDouble(BigDecimalLng.toEngineeringString());
            double latitude = Double.parseDouble(BigDecimalLat.toEngineeringString());
            Point point=new Point(longitude,latitude);
            pointMap.put(String.valueOf(stationPO.getId()),point);

            String stationInfoKey=DeviceCacheKey.STATION_DETAIL_STRING_KEY+stationPO.getId();
            String json = JsonUtils.toJson(stationPO);
            valueOperations.set(stationInfoKey,json);
            log.debug("通过Redis基本类型String 保存场站详情:key:{},value:{}",stationInfoKey,json);

            Map<String,Object> smallMap=new HashMap<>();
            String smallMapKey=DeviceCacheKey.STATION_DETAIL_HASH_KEY+stationPO.getId();
            smallMap.put("name",stationPO.getStationName());
            smallMap.put("deviceNumber",stationPO.getDeviceNumber());
            smallMap.put("operatorId",stationPO.getOperatorId());
            hashOperations.putAll(smallMapKey,smallMap);
            log.debug("通过Redis基本类型Hash 保存场站详情:key:{},value:{}",stationInfoKey,json);
        }
        station.add(pointMap);
        log.debug("缓存预热完成,成功将场站的位置信息放入RedisGEO,场站基本信息放入Redis-String,场站基本信息放入Redis-Hash,体会不同类型的用法");
    }
}
